﻿Imports System.Data
Partial Class Content_Reportes_rep_controls_AsignacionInstitucion
    Inherits System.Web.UI.UserControl
    Dim ECConnString As String = ConfigurationManager.AppSettings("dbIP")
    Dim IControles As New Clases.controles
    Dim IDataBase As New Clases.DataBase
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Dim q1, q2, q3 As String
            q1 = "Select * from TDespachos where stat_id=1"
            IControles.fill_dropDownListBaseQuery(ddlDespacho, "TDespachos", "desp_id", "desp_nombre", q1)
            q2 = "Select * from TInstituciones where stat_id=1"
            IControles.fill_dropDownListBaseQuery(ddlInstitucion, "TInstituciones", "inst_id", "inst_nombre", q2)
            IControles.addNoneAllElement(ddlInstitucion)
            q3 = "Select * from tEmisoras where stat_id=1"
            IControles.fill_dropDownListBaseQuery(ddlEmisora, "tEmisoras", "emi_id", "emi_nombre", q3)
            IControles.addNoneAllElement(ddlEmisora)

            IControles.addNoneAllElement(ddlDespacho)

            txtFechaA.Attributes.Add("onKeyPress", "return false;")
            txtFechaA.Attributes.Add("onPaste", "return false;")

            txtFechaOp.Attributes.Add("onKeyPress", "return false;")
            txtFechaOp.Attributes.Add("onPaste", "return false;")

        End If

    End Sub


    Protected Sub ImageButton1_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ImageButton1.Click
        Dim q1 As String
        Dim q2 As String
        q1 = "select * from vwAsignacionInst where 1=1"
        q2 = "select sum(num_titulos) as suma from vwAsignacionInst where 1=1"

        If ddlDespacho.SelectedValue <> "None" Then
            q1 = q1 & " and desp_id=" & ddlDespacho.SelectedValue & ""
            q2 = q2 & " and desp_id=" & ddlDespacho.SelectedValue & ""
        End If

        If ddlInstitucion.SelectedValue <> "None" Then
            q1 = q1 & " and inst_id=" & ddlInstitucion.SelectedValue & ""
            q2 = q2 & " and inst_id=" & ddlInstitucion.SelectedValue & ""
        End If

        If ddlEmisora.SelectedValue <> "None" Then
            q1 = q1 & " and emi_id=" & ddlEmisora.SelectedValue & ""
            q2 = q2 & " and emi_id=" & ddlEmisora.SelectedValue & ""
        End If

        If txtFechaOp.Text IsNot "" And txtFechaA.Text IsNot "" Then
            q1 = q1 & " AND [Fecha de Operación] >= '" & txtFechaOp.Text & "' AND [Fecha de Operación] <= '" & txtFechaA.Text & "'"
            q2 = q2 & " AND [Fecha de Operación] >= '" & txtFechaOp.Text & "' AND [Fecha de Operación] <= '" & txtFechaA.Text & "'"
        ElseIf txtFechaOp.Text IsNot "" Then
            q1 = q1 & " AND [Fecha de Operación] >= '" & txtFechaOp.Text & "'"
            q2 = q2 & " AND [Fecha de Operación] >= '" & txtFechaOp.Text & "'"
        ElseIf txtFechaA.Text IsNot "" Then
            q1 = q1 & " AND [Fecha de Operación] <= '" & txtFechaA.Text & "'"
            q2 = q2 & " AND [Fecha de Operación] <= '" & txtFechaA.Text & "'"
        End If


        If lb_tipoO.SelectedValue = "Compra" Then
            q1 = q1 & " and (mov_operacion='Entrada Fisica' or mov_operacion='Compra')"
            q2 = q2 & " and (mov_operacion='Entrada Fisica' or mov_operacion='Compra')"
        Else
            q1 = q1 & " and (mov_operacion='Salida Fisica' or mov_operacion='Venta')"
            q2 = q2 & " and (mov_operacion='Salida Fisica' or mov_operacion='Venta')"
        End If



        hf_institucion.Value = q1
        Session("RepMovi") = q1
        IControles.fillGridViewPaging(gvReporteInvDisponibleCompra, q1)
        btn_excel.Visible = True
        titulosT.Visible = True
        titulosT.Text = IDataBase.getSimpleData(q2, "suma")
        Label5.Visible = True
    End Sub

    Protected Sub gvReporteInvDisponibleCompra_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gvReporteInvDisponibleCompra.PageIndexChanging
        IControles.fillGridViewPaging(gvReporteInvDisponibleCompra, hf_institucion.Value, Convert.ToInt32(e.NewPageIndex))
    End Sub

    Protected Sub exportToExcel(ByVal query As String)
        Dim gridView_toExport As New GridView
        IControles.fillGridViewPaging(gridView_toExport, Session("RepMovi"))
        For i As Integer = 0 To gridView_toExport.Rows.Count - 1
            Dim row As GridViewRow = gridView_toExport.Rows(i)
        Next
        Dim sb As StringBuilder = New StringBuilder()
        Dim sw As IO.StringWriter = New IO.StringWriter(sb)
        Dim htw As HtmlTextWriter = New HtmlTextWriter(sw)
        Dim pagina As Page = New Page
        Dim form = New HtmlForm
        pagina.EnableEventValidation = False
        pagina.DesignerInitialize()
        pagina.Controls.Add(form)
        gridView_toExport.AllowPaging = False
        form.Controls.Add(gridView_toExport)
        pagina.RenderControl(htw)
        Response.Clear()
        Response.Buffer = True
        Response.ContentType = "application/vnd.ms-excel"
        Response.AddHeader("Content-Disposition", "attachment;filename=AsigInst" & Now.ToString("_dd_MMMM_yy_HH_mm_ss") & ".xls")
        Response.Charset = "UTF-8"
        Response.ContentEncoding = Encoding.Default
        Response.Write(sb.ToString())
        Response.End()

    End Sub

    Protected Sub btn_excel_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btn_excel.Click
        exportToExcel(Session("RepMovi"))
    End Sub

End Class
